<?php
/**
 *  订单商品统计的
 *  统计起始时间
 *  统一在此维护
 *
 *  包括订单，订单商品，订单售后，
 *  这些统计时间是一致的，统一在此
 *
 *  @author jincai
 */

namespace app\statis\lib;

use app\common\helper\Time;

class TimeStatistic {

    /*统计的时间偏移量*/
    const TIME_OFFSET = 20*3600;

    /**
     * 订单统计的
     * 开始时间
     * @param null $time 年月日的字符串
     * 示例：'2019-09-11'
     *
     * @return false|int
     */
    public static function getStartTime($time = null)
    {
        if (empty($time)) {
            $time = Time::today(Time::TYPE_INT);
        } else {
            $time = strtotime($time);
        }

        $start_time = strtotime('-1 day', $time);
        $start_time = $start_time + self::TIME_OFFSET;
        return $start_time;
    }

    /**
     * 订单统计
     * 结束时间
     * @param null $time 年月日的字符串
     * @return false|int
     */
    public static function getEndTime($time = null)
    {
        if (empty($time)) {
            $time = Time::today(Time::TYPE_INT);
        } else {
            $time = strtotime($time);
        }

        $end_time = $time + self::TIME_OFFSET;
        return $end_time;
    }

    /**
     * 获取订单统计的时间
     *
     * @param int $stat_end 统计截止时间
     *
     * @return false|string
     */
    public static function getStatTime($stat_end)
    {
        $cur_time = Time::currentDay($stat_end, Time::TYPE_INT);
        if ($stat_end > ($cur_time + 20*3600)) {
            $stat_time = strtotime('+1 day', $cur_time);
        } else {
            $stat_time = $cur_time;
        }
        $stat_time = date('Y-m-d', $stat_time);

        return $stat_time;
    }

}